//
//  ActiveDetailTopCell.m
//  yunyan
//
//  Created by haichuang on 2018/3/15.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "ActiveDetailTopCell.h"

@implementation ActiveDetailTopCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createTopCell];
        [self initTopView];
    }
    return self;
}
-(void)createTopCell{
    
    //背景
    UIView * superView = self;
   
    //顶部的header
      _shHeardView = [[UIView alloc]init];
    [superView addSubview:_shHeardView];
    [_shHeardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo (superView.mas_top).offset (10.f);
        make.left.right.equalTo(superView);
        make.height.equalTo (@(66));
     
    }];
    

   //浅灰色的 背景
    _bgView = [[UIView alloc]init];
    [superView addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_shHeardView);
        make.top.equalTo(_shHeardView.mas_bottom).offset (10.f);
//        make.height.equalTo (@(100));
//        make.bottom.equalTo (superView.mas_bottom);
    }];
    _bgView.backgroundColor = RGBA(243, 245, 248, 1);
//
    
    //文章的title
    _titlelLabel = [[UILabel alloc] init];
    [_bgView addSubview:_titlelLabel];
    [_titlelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo (superView.mas_left).offset (10.f);
        make.right.equalTo (superView.mas_right).offset (-10.f);
        make.top.equalTo (_bgView.mas_top).offset (18.f);
    }];
    _titlelLabel.textColor = RGBA(51, 51, 51, 1);
    _titlelLabel.font = [UIFont systemFontOfSize:18.0f];
    _titlelLabel.text = @"黔南第一山 - 弘福寺";
    _titlelLabel.textAlignment = NSTextAlignmentCenter;
    _titlelLabel.numberOfLines = 0;
    
  
    
    
    UIView * imageBGview = [[UIView alloc] init];
    [_bgView addSubview:imageBGview];

    [imageBGview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo (_titlelLabel.mas_bottom).offset (10.f);
        make.left.right.equalTo (_titlelLabel);
   
    }];

    self.imageArray = USER_DEFAULTGet(@"actDetailsIamgeArray");
    //此处可以传view数组
    NSMutableArray * viewARR = [NSMutableArray array];
    for (int a = 0; a < self.imageArray.count; a ++) {
      _image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
      
        [viewARR addObject:_image];

    }


    //关于这个延迟 因为masory创建约束之后 获取的frame 是（0，0，0，0）但是有一个延迟的话 才能获取真是的view的大小  延迟时间改成0 也可以

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       // NSLog(@"******%@",contenview);
    });

    UIView * lastView ;
  
  
    for (int i = 0; i < viewARR.count ; i++) {
        UIImageView * view = viewARR[i];
        view.userInteractionEnabled = YES;
        // 添加手势
        if(view.gestureRecognizers.count == 0) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageAction:)];
            [view addGestureRecognizer:tap];
            tap.view.tag = i;
        }
          [imageBGview addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo (imageBGview.mas_left);
            make.right.equalTo (imageBGview.mas_right);
            make.top.equalTo(lastView ? lastView.mas_bottom: @0).offset(10);
            make.height.equalTo (view.mas_width).multipliedBy (0.5);
        }];
        [view sd_setImageWithURL:ImageUrl(self.imageArray[i]) placeholderImage:ImageName(@"")];;
         view.contentMode=UIViewContentModeScaleAspectFill;
        view.clipsToBounds=YES;
        lastView = view;
        
      
    }

    [imageBGview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView.mas_bottom);
    }];
    
    
    //内容详情
    _detailLabel = [[UILabel alloc]init];
    [_bgView addSubview:_detailLabel];
    [_detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        if ( self.imageArray.count==0) {
             make.top.equalTo (_titlelLabel.mas_bottom).offset (20.f);
        }else {
            
             make.top.equalTo (imageBGview.mas_bottom).offset (20.f);
        }
       
        make.left.equalTo(_titlelLabel.mas_left).offset (10.0);
        make.right.equalTo(_titlelLabel.mas_right).offset (-10.0);
        
    }];
    
    _detailLabel.numberOfLines = 0;
    _detailLabel.textColor = RGBA(51, 51, 51, 1);
    _detailLabel.text = @"  弘福寺位于贵阳市黔灵山群峰中心，距城约1.5公里，是十方丛林，为贵州首刹，向有“黔南第一山”之称。弘福寺于1672年（清康熙11年）由赤松和尚开创，“弘福”二字乃“弘佛大愿，救人救世；福我众生，善始善终之意。赤松是为本寺开山始祖，佛法为临济一系之正宗，乃禅门五宗之一。";
    _detailLabel.font = [UIFont  systemFontOfSize:16];
    _detailLabel.numberOfLines = 0;
    
     _likeBtn = [[UIButton alloc] init];
    [_bgView addSubview:_likeBtn];
    _bgView.layer.masksToBounds = YES;
    _bgView.layer.cornerRadius = 5;
    [_likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo (@(30.f));
        make.width.equalTo (@(105.0f));
        make.centerX .equalTo (_detailLabel.mas_centerX);
        make.top.equalTo (_detailLabel.mas_bottom).offset(20.0f);
    }];
    _likeBtn.layer.masksToBounds = YES;
    _likeBtn.layer.cornerRadius = 5;
    
     _likeBtn.backgroundColor = [UIColor whiteColor];
      [_likeBtn setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal];
     _likeBtn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
     [_likeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 12, 0, 0)];
     [_likeBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
     [_likeBtn setTitle:@"点赞" forState:UIControlStateNormal];
    
    int sele = [USER_DEFAULTGet(@"act_is_fabulous") intValue];
    if (sele ==1) {
         [_likeBtn setImage:[UIImage imageNamed:@"new_praise_pressed"] forState:UIControlStateNormal];
    }else{
         [_likeBtn setImage:[UIImage imageNamed:@"new_praise_default"] forState:UIControlStateNormal];
        
    }

    [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo (_likeBtn.mas_bottom).offset (20.0f);
        make.bottom.equalTo (superView.mas_bottom);
    }];

}

- (void)tapImageAction:(UITapGestureRecognizer *)tap {
    
    NSLog(@"tap.view.tag = %ld",tap.view.tag);
    NSString * tapTag = [NSString stringWithFormat:@"%ld",tap.view.tag];
     [[NSNotificationCenter  defaultCenter] postNotificationName:@"imageTap" object:tapTag];
//    TCLPhotoBrowserViewController *photo = [[TCLPhotoBrowserViewController alloc]init];
//    photo.photos = self.imageArray;
//    photo.currentPage = tap.view.tag;
//    [photo presentViewController:photo animated:YES completion:nil];
}

-(void)likeBtnClicked{
    
//    if (_isSelected) {
//        [_likeBtn setImage:[UIImage imageNamed:@"new_praise_pressed"] forState:UIControlStateNormal];
//        _isSelected = NO;
//    }else{
//       [_likeBtn setImage:[UIImage imageNamed:@"new_praise_default"] forState:UIControlStateNormal];
//        _isSelected = YES;
//    }
}

-(void)initTopView{
    WeakSelf;
    self.backgroundColor = [UIColor whiteColor];
    _headImage = [[UIImageView alloc] init];
    [_shHeardView addSubview:_headImage];
    [_headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo (weakSelf).offset (14.0f);
        make.height.width.equalTo (@(40.0f));
    }];
    _headImage.layer.masksToBounds = YES;
    _headImage.layer.cornerRadius = 20.0f;
    _headImage.image = [UIImage imageNamed:@"jjlin.jpg"];;
    
    
//    _nameTitleLabel = [[UILabel alloc] init];
//    [_shHeardView addSubview:_nameTitleLabel];
//    [_nameTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.bottom.equalTo (_headImage);
//        make.left.equalTo (_headImage.mas_right).offset (9.0f);
//    }];
//    _nameTitleLabel.textColor = RGBA(51, 51, 51, 1);
//    _nameTitleLabel.font = [UIFont systemFontOfSize:18.0f];
//    _nameTitleLabel.text = USER_DEFAULTGet(@"act_user");
//    
//    _timeLabel = [[UILabel alloc] init];
//    [_shHeardView addSubview:_timeLabel];
//    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.bottom.equalTo (_headImage);
//        make.right.equalTo (weakSelf.mas_right).offset (-20.0f);
//    }];
//    _timeLabel.textColor = RGBA(153, 153, 153, 1);
//    _timeLabel.font = [UIFont systemFontOfSize:12.0f];
//    _timeLabel.text = USER_DEFAULTGet(@"act_time");
}




@end
